//
//  XMLParser.m
//  TestConnection
//
//  Created by Boc on 8/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "XMLParserDelegate.h"


@implementation XMLParserDelegate
@synthesize delegate;
-(id) init
{
    self = [super init];
    if (self) {
        _moviesDictionary = [MoviesDictionary instance];
        isParsing = NO;
    }
    return self;
}
-(void)dealloc
{
    [_moviesDictionary release];
    [super dealloc];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"started to parse");
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"stopped parsing");
    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ParseReady" object:nil];
    //[[self delegate]ParseReady];

         
}

-(bool)isAnElementToParse: (NSString*)element
{
    if ([element isEqualToString:@"Name"] ||
        [element isEqualToString:@"Year"] ||
        [element isEqualToString:@"Director"])
        
    {
        return YES;
    }
    
    return NO;
}

-(void) saveElement:(NSString*)element value:(NSString*)value
{
    //NSRange range = [value rangeOfString:@"^\\s*" options:NSRegularExpressionSearch];
    //value = [value stringByReplacingCharactersInRange:range withString:@""];
    
    if ([element isEqualToString:@"Name"])
    {
        [movieDicAux setObject:value forKey:@"Name"];
        //movieAux.name = value;
        
    }else if ([element isEqualToString:@"Year"])
    {
        [movieDicAux setObject:value forKey:@"Year"];        
        //movieAux.year = [NSNumber numberWithInt:[value intValue]];
        
    }else if ([element isEqualToString:@"Director"])
    {
        //movieAux.language_name = value;
        [movieDicAux setObject:value forKey:@"Director"];
    }
     
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"%@ START: %d", elementName,[attributeDict count]);
    
    if ([elementName isEqualToString:@"movie"])
    {
        movieDicAux = [[NSMutableDictionary alloc]init];
        //movieAux = [[Movie alloc]init];
        
    } else if ([self isAnElementToParse:elementName])
    {
        isParsing = YES;
        parsingValue = [[NSString alloc]init];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"%@ END", elementName);
    
    if ([self isAnElementToParse:elementName]) {
        isParsing = NO;
        [self saveElement:elementName value:parsingValue];
        [parsingValue release];
                

    }else if ([elementName isEqualToString:@"movie"])
    {
        [_moviesDictionary setMovie:movieDicAux forKey:[movieDicAux objectForKey:@"Name"]];
        [movieDicAux release];    
    }

}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"foundCharacters: %@", string);
    if (isParsing && string) {
        parsingValue = [parsingValue stringByAppendingString:string];
        
    }
}

@end
